//
//  MainViewController.swift
//  阿酷微博
//
//  Created by co on 16/3/10.
//  Copyright © 2016年 王南侠. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
        setupTabbar()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubviewToFront(composeButton)
        
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
    }
    
    // MARK: - 监听方法
    @objc func clilckCopmoseButton() {
        print("点击了撰写按钮")
    }
    
    
    // MARK: - 私有控件
    private lazy var composeButton: UIButton = UIButton(
        cz_title: nil,
        imageName: "tabbar_compose_icon_add",
        backImageName: "tabbar_compose_button")
    

}

// MARK: - 设置tabbar
extension MainViewController {
    
    private func setupTabbar() {
        
        tabBar.shadowImage = UIImage.cz_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        tabBar.addSubview(composeButton)
        
        composeButton.addTarget(self, action: "clilckCopmoseButton", forControlEvents: .TouchUpInside)
        
    }
    
}



// MARK: -
extension MainViewController {
    
   
    private func addChildViewControllers() {
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(UIViewController())
        
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")

        
    }
    /// 添加一个控制器
    ///
    /// - parameter vc:        视图控制器
    /// - parameter title:     标题
    /// - parameter imageName: 图像名称
    
    private func addChildViewController(vc: UIViewController,title: String, imageName: String) {
        
        vc.title = title
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState:.Selected )
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(10)], forState: .Normal)

        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        let nav = UINavigationController(rootViewController: vc)
        
        addChildViewController(nav)
        
        
    }
    
}